package Year22_Month06.day23_剑Off3;

public class 和大于target的最短子数组 {
    public int minSubArrayLen(int target, int[] nums) {
        int length = Integer.MAX_VALUE;//记录最小数组的长度，直接设置为最大
        int pre = 0;//滑动窗口起始指针
        int num = 0;//记录当前滑动窗口的和
        for (int i = 0; i < nums.length; i++) {
            num += nums[i];
            while(num >= target){
                length = Math.min(length,i - pre + 1);
                num -= nums[pre];
                pre++;
            }
        }
        return length == Integer.MAX_VALUE ? 0 : length;
    }
}
